package GEMonitor;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class GEServer
{
    static final int TCP_PORT = Integer.parseInt(System.getProperty("port", "8322"));

    public static void main(String[] args) {
        MyLogger.Info("programe starting...");
        
        GEChannelMgr.getInstance().start();

        startTcpServer();
    }

    static void startTcpServer()
    {
        MyLogger.Info("tcp server is starting...");

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
//             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new GEServerInitializer());

            b.bind(TCP_PORT).sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
            MyLogger.Error("tcp server starting failed!");
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}